﻿using System;


namespace WoogiWorld.Event
{
    public delegate void EventFunction(EventMessage info);

    public class EventListener
    {
        public object listener;
        public EventFunction function;

        ~EventListener()
        {
            function = null;
        }

        public void Dispose()
        {
            DisposeLua();
            function = null;
            listener = null;
        }

        public void DisposeLua()
        {
        }
    }

    public struct EventMessage
    {
        private EventListener target;
        private string eventType;

        public EventListener Target
        {
            get { return target; }
        }

        public string EventType
        {
            get { return eventType; }
        }

        // 传多参 ////////////////////////////////////////////////////////////////////////////////////////////////////////
        private object[] _newdata;

        public EventMessage(EventListener target, string type, object[] data)
        {
            this.target = target;
            this.eventType = type;
            this._newdata = data;
        }

        public bool CheckNewData()
        {
            return _newdata != null && _newdata.Length > 0;
        }

        public object[] NewData
        {
            get { return _newdata; }
        }

        public int Length => _newdata != null ? _newdata.Length : 0;

        public T ReadNewData<T>(T w_t = default(T))
        {
            if (0 < Length)
            {
                return (T)_newdata[0];
            }

            return w_t;
        }

        public T ReadNewData<T>(int _idx, T w_t = default(T))
        {
            if (_idx >= 0 && _idx < Length)
            {
                return (T)Convert.ChangeType(_newdata[_idx], typeof(T));
            }

            return w_t;
        }
    }
}